﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using Maticsoft.DBUtility;
namespace DAL  
{
	 	//StockAlert
		public partial class StockAlertDAL
	{
   		     
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from StockAlert");
			strSql.Append(" where ");
			                                       strSql.Append(" Id = @Id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Common.Model.StockAlert model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into StockAlert(");			
            strSql.Append("CustomerId,Code,UpAlertPrice,UpAlertMargin,UpAuto,UPChangeTime,DownAlertPrice,DownAlertMargin,DownAuto,DownChangeTime,CreateTime");
			strSql.Append(") values (");
            strSql.Append("@CustomerId,@Code,@UpAlertPrice,@UpAlertMargin,@UpAuto,@UPChangeTime,@DownAlertPrice,@DownAlertMargin,@DownAuto,@DownChangeTime,@CreateTime");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@CustomerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Code", SqlDbType.VarChar,10) ,            
                        new SqlParameter("@UpAlertPrice", SqlDbType.Float,8) ,            
                        new SqlParameter("@UpAlertMargin", SqlDbType.Float,8) ,            
                        new SqlParameter("@UpAuto", SqlDbType.Float,8) ,            
                        new SqlParameter("@UPChangeTime", SqlDbType.SmallDateTime) ,            
                        new SqlParameter("@DownAlertPrice", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownAlertMargin", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownAuto", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownChangeTime", SqlDbType.SmallDateTime) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime)             
              
            };
			            
            parameters[0].Value = model.CustomerId;                        
            parameters[1].Value = model.Code;                        
            parameters[2].Value = model.UpAlertPrice;                        
            parameters[3].Value = model.UpAlertMargin;                        
            parameters[4].Value = model.UpAuto;                        
            parameters[5].Value = model.UPChangeTime;                        
            parameters[6].Value = model.DownAlertPrice;                        
            parameters[7].Value = model.DownAlertMargin;                        
            parameters[8].Value = model.DownAuto;                        
            parameters[9].Value = model.DownChangeTime;                        
            parameters[10].Value = model.CreateTime;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Common.Model.StockAlert model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update StockAlert set ");
			                                                
            strSql.Append(" CustomerId = @CustomerId , ");                                    
            strSql.Append(" Code = @Code , ");                                    
            strSql.Append(" UpAlertPrice = @UpAlertPrice , ");                                    
            strSql.Append(" UpAlertMargin = @UpAlertMargin , ");                                    
            strSql.Append(" UpAuto = @UpAuto , ");                                    
            strSql.Append(" UPChangeTime = @UPChangeTime , ");                                    
            strSql.Append(" DownAlertPrice = @DownAlertPrice , ");                                    
            strSql.Append(" DownAlertMargin = @DownAlertMargin , ");                                    
            strSql.Append(" DownAuto = @DownAuto , ");                                    
            strSql.Append(" DownChangeTime = @DownChangeTime , ");                                    
            strSql.Append(" CreateTime = @CreateTime  ");            			
			strSql.Append(" where Id=@Id ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@CustomerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Code", SqlDbType.VarChar,10) ,            
                        new SqlParameter("@UpAlertPrice", SqlDbType.Float,8) ,            
                        new SqlParameter("@UpAlertMargin", SqlDbType.Float,8) ,            
                        new SqlParameter("@UpAuto", SqlDbType.Float,8) ,            
                        new SqlParameter("@UPChangeTime", SqlDbType.SmallDateTime) ,            
                        new SqlParameter("@DownAlertPrice", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownAlertMargin", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownAuto", SqlDbType.Float,8) ,            
                        new SqlParameter("@DownChangeTime", SqlDbType.SmallDateTime) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime)             
              
            };
            			            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.CustomerId;                        
            parameters[2].Value = model.Code;                        
            parameters[3].Value = model.UpAlertPrice;                        
            parameters[4].Value = model.UpAlertMargin;                        
            parameters[5].Value = model.UpAuto;                        
            parameters[6].Value = model.UPChangeTime;                        
            parameters[7].Value = model.DownAlertPrice;                        
            parameters[8].Value = model.DownAlertMargin;                        
            parameters[9].Value = model.DownAuto;                        
            parameters[10].Value = model.DownChangeTime;                        
            parameters[11].Value = model.CreateTime;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from StockAlert ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 删除几条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from StockAlert ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Common.Model.StockAlert GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id, CustomerId, Code, UpAlertPrice, UpAlertMargin, UpAuto, UPChangeTime, DownAlertPrice, DownAlertMargin, DownAuto, DownChangeTime, CreateTime  ");			
			strSql.Append("  from StockAlert ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;

			
			Common.Model.StockAlert model=new Common.Model.StockAlert();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["CustomerId"].ToString()!="")
				{
					model.CustomerId=int.Parse(ds.Tables[0].Rows[0]["CustomerId"].ToString());
				}
																																				model.Code= ds.Tables[0].Rows[0]["Code"].ToString();
																												if(ds.Tables[0].Rows[0]["UpAlertPrice"].ToString()!="")
				{
					model.UpAlertPrice=decimal.Parse(ds.Tables[0].Rows[0]["UpAlertPrice"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["UpAlertMargin"].ToString()!="")
				{
					model.UpAlertMargin=decimal.Parse(ds.Tables[0].Rows[0]["UpAlertMargin"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["UpAuto"].ToString()!="")
				{
					model.UpAuto=decimal.Parse(ds.Tables[0].Rows[0]["UpAuto"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["UPChangeTime"].ToString()!="")
				{
					model.UPChangeTime=DateTime.Parse(ds.Tables[0].Rows[0]["UPChangeTime"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DownAlertPrice"].ToString()!="")
				{
					model.DownAlertPrice=decimal.Parse(ds.Tables[0].Rows[0]["DownAlertPrice"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DownAlertMargin"].ToString()!="")
				{
					model.DownAlertMargin=decimal.Parse(ds.Tables[0].Rows[0]["DownAlertMargin"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DownAuto"].ToString()!="")
				{
					model.DownAuto=decimal.Parse(ds.Tables[0].Rows[0]["DownAuto"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DownChangeTime"].ToString()!="")
				{
					model.DownChangeTime=DateTime.Parse(ds.Tables[0].Rows[0]["DownChangeTime"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}

        public DataSet GetUsedStockList(String strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct Code from stockalert ");
            return DbHelperSQL.Query(strSql.ToString());
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM StockAlert ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM StockAlert ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
	}
}

